
<?php

class Lances_Arrematados_M extends I_Model {

    protected $_table_name = 'lances_arrematados';
    protected $_primary_key = 'ID';
    protected $_primary_filter = 'intval';
    protected $_order_by = 'ID';
    public $rules = array();
    protected $_timestamps = FALSE;

    public function __construct() {
        parent::__construct();
    }

    function get_user_info($id) {

        $this->db->select('le.TITLE,cm.NOME_FANTASIA,pg.KEY AS HASH_BOLETO,'
                . 'pg.PAGAMENTO_ENVIADO,pg.CONFIRMADO,pg.NFE_LIBERADO,'
                . 'lt.NUM_LOTE,lt.DESCRICAO,lc.COMISSAO,lt.LEILAO_ID,'
                . 'pr.APROUVE,pr.ID as PROPOSTA_ID,'
                . 'lc.TOTAL_COMISSAO,lca.*')
                ->from('lances_arrematados as lca')
                ->join('lances as lc', 'lca.LANCE_ID=lc.ID')
                ->join('lotes as lt', 'lc.LOTE_ID=lt.ID')
                ->join('lances_propostas as pr', 'lc.LOTE_ID=pr.LOTE_ID AND lc.USER_ID=pr.USER_ID','LEFT')
                ->join('leiloes as le', 'lt.LEILAO_ID=le.ID')
                ->join('lances_pagamentos as pg', 'pg.PAGAMENTO_ID=lca.PAGAMENTO_ID','LEFT')
                ->join('comitentes as cm', 'cm.ID=le.COMITENTE_ID','RIGHT')
                ->where('lc.USER_ID', $id)
                ->order_by('lc.ID', 'DESC');


        $q = $this->db->get();
        return $q->result();
    }

    function get_by_user_lote_leilao($uid, $ltid, $leid) {

        $this->db->select('cm.*,le.DATA AS DATA_LEILAO,pg.KEY AS HASH_BOLETO,pg.CONFIRMADO, lc.COMISSAO,lc.TOTAL_COMISSAO,lt.DESCRICAO,lt.NUM_LOTE,lca.*')
                ->from('lances_arrematados as lca')
                ->join('lances as lc', 'lca.LANCE_ID=lc.ID')
                ->join('leiloes as le', 'lc.LEILAO_ID=le.ID')
                ->join('lances_pagamentos as pg', 'pg.PAGAMENTO_ID=lca.PAGAMENTO_ID','LEFT')
                ->join('lotes as lt', 'lc.LOTE_ID=lt.ID')
                ->join('comitentes as cm', 'cm.ID=le.COMITENTE_ID')
                ->where(array(
                    'lca.USER_ID' => $uid,
                    'lca.LOTE_ID' => $ltid,
                    'lca.LEILAO_ID' => $leid));

        $q = $this->db->get();

        return $q->result();
    }

    function info_lote($where) {

        $this->db->select('cm.*,p.*,le.DATA AS DATA_LEILAO,lc.COMISSAO,lc.TOTAL_COMISSAO,lt.DESCRICAO,lt.NUM_LOTE,lca.*')
                ->from('lances_arrematados as lca')
                ->join('lances as lc', 'lca.LANCE_ID=lc.ID')
                 ->join('leiloes as le', 'le.ID=lc.LEILAO_ID')
                ->join('lotes as lt', 'lc.LOTE_ID=lt.ID')
                ->join('profiles as p', 'p.USER_ID=lc.USER_ID')
                ->join('comitentes as cm', 'cm.ID=le.COMITENTE_ID')
                ->where($where);

        $q = $this->db->get();

        return $q->result();
    }
    
    function by_lance($id){
   return     $this->db->select('lt.DESCRICAO,lt.NUM_LOTE,us.LOGIN,le.*,cm.*,lca.*')
                 ->from('lances_arrematados as lca')
                ->join('lotes as lt','lt.ID=lca.LOTE_ID')
                ->join('leiloes as le','le.ID=lca.LEILAO_ID')
                ->join('comitentes as cm','le.COMITENTE_ID=cm.ID')
                ->join('users as us','us.ID=lca.USER_ID')
                ->where(array('lca.LANCE_ID'=>$id))
                ->get()
                ->row();
    }

}
